<template>
  <div>
    <div class="content">
      <el-form :model="loginParam" :rules="rules2" ref="loginParam" label-width="100px" class="demo-ruleForm"
      style="padding-top: 20px">
        <el-form-item label="账号" prop="account" style="width:500px;margin: 0 auto">
          <el-input v-model.number="loginParam.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width:500px;margin: 20px auto 0 auto;">
          <el-input type="password" v-model="loginParam.password"></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 20px">
          <el-button type="primary" @click="submitForm('loginParam')">提交</el-button>
          <el-button @click="resetForm('loginParam')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style lang="less" rel="stylesheet/less">
  @import "login.css";
</style>
<script>
  export default{
    components: {
    },
    data: function () {
      let checkAccount = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('账号不能为空'))
        } else {
          callback()
        }
      }
      let checkPassword = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('密码不能为空'))
        } else if (value.length < 6) {
          return callback(new Error('密码不能少于六位'))
        } else {
          callback()
        }
      }
      return {
        loginParam: {
          account: '',
          password: ''
        },
        rules2: {
          account: [
            { validator: checkAccount, trigger: 'blur', required: true }
          ],
          password: [
            { validator: checkPassword, trigger: 'blur', required: true }
          ]
        }
      }
    },
    events: {},
    methods: {
      resetForm: function (name) {
        this.$refs[name].resetFields()
      },
      submitForm: function (name) {
        let self = this
        this.$refs[name].validate(res => {
          if (res) {
            this.$http.get('../../static/login.json').then(res => {
              if (this.loginParam.account === res.data.data.account && this.loginParam.password === res.data.data.password) {
                self.$message({
                  message: '登录成功',
                  type: 'success'
                })
                setTimeout(() => {
                  self.$router.push('/layout')
                }, 800)
              } else {
                self.$message({
                  message: '账号或密码有误',
                  type: 'error'
                })
              }
            }).catch(error => {
              console.log(error)
            })
          }
        })
      }
    }
  }
</script>
